package cn.hxzy.icon.icon.controller;


import cn.hxzy.icon.icon.entity.Icon;
import cn.hxzy.icon.icon.entity.IconGroup;
import cn.hxzy.icon.icon.entity.User;
import cn.hxzy.icon.icon.entity.po.IconPo;
import cn.hxzy.icon.icon.entity.vo.PageBean;
import cn.hxzy.icon.icon.entity.vo.ReturnBean;
import cn.hxzy.icon.icon.service.IconService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.security.acl.Group;
import java.util.Base64;

/**
 * @author little black
 */
@RestController
@RequestMapping("icon")
public class IconController {
    @Autowired
    private IconService iconService;

    /**
     * 查询全部信息
     *
     * @param page
     * @param size
     * @return
     */
    @PostMapping("list")
    public ReturnBean list(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "8") Integer size) {
        PageBean<Icon> all = iconService.findAll(new PageBean<Icon>(page, size));
        return ReturnBean.of(ReturnBean.AnswerCode.SUCCESS, all);
    }

    /**
     * 模糊插询
     * @param page
     * @param size
     * @param name
     * @return
     */
    @PostMapping("findByName")
    public ReturnBean findByName(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "8") Integer size, String name) {
        PageBean<Icon> all = iconService.findByNameLike(new PageBean<Icon>(page, size), name);
        return ReturnBean.of(ReturnBean.AnswerCode.SUCCESS, all);
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @PostMapping("deleteId")
    public ReturnBean delete(Integer id) {
        iconService.deleteById(id);
        return ReturnBean.of(ReturnBean.AnswerCode.SUCCESS);
    }

    /**
     * 添加
     * @param name
     * @param orders
     * @param groupId
     * @param file
     * @return
     * @throws IOException
     */
    @PostMapping("add")
    public ReturnBean add(String name, Integer orders, Integer groupId, MultipartFile file) throws IOException {
        Icon icon = new Icon();
        IconGroup iconGroup = new IconGroup();
        iconGroup.setId(groupId);
        icon.setName(name);
        icon.setOrders(orders);
        icon.setGroup(iconGroup);
        icon.setImgByte(file.getBytes());
        iconService.add(icon);
        return ReturnBean.of(ReturnBean.AnswerCode.SUCCESS);
    }

    /**
     * id查询
     * @param id
     * @return
     */
    @PostMapping("findById")
    public ReturnBean findById(Integer id) {
        Icon icon =iconService.FindById(id);
        return ReturnBean.of(ReturnBean.AnswerCode.SUCCESS, icon);
    }

    /**
     * 修改
     * @param name
     * @param orders
     * @param groupId
     * @param file
     * @param iconPo
     * @param id
     * @return
     * @throws IOException
     */
    @PostMapping("update")
    public ReturnBean update(String name, Integer orders, Integer groupId, MultipartFile file, IconPo iconPo,Integer id) throws IOException {
        Icon icon = new Icon();
        IconGroup iconGroup = new IconGroup();
        iconGroup.setId(groupId);
        iconPo.setId(id);
        iconPo.setName(name);
        iconPo.setOrders(orders);
        iconPo.setGroup(iconGroup);
        iconPo.setImgByte(file.getBytes());
        BeanUtils.copyProperties(iconPo, icon);
        iconService.update(icon);
        return ReturnBean.of(ReturnBean.AnswerCode.SUCCESS);
    }
}
